'ui';
configure_page = ui.inflate(
    <vertical>
        <vertical layout_weight="9">
            <text id="biaoti" text="设置" gravity="center" />
            <linear>
                <text text="清空多触点时间(m)" />
                <input id="time_reload" textSize="12" inputType="number" width="100px" singleLine="true" />
            </linear>
            <linear>
                <text text="打马贼超时时间(s)" />
                <input id="outTime_dmz" textSize="12" inputType="number" width="100px" singleLine="true" />
                <text text="找马贼超时时间(s)" />
                <input id="outTime_zmz" textSize="12" inputType="number" width="100px" singleLine="true" />
            </linear>
            <linear>
                <text text="多人副本超时时间(s)" />
                <input id="outTime_drfb" textSize="12" inputType="number" width="100px" singleLine="true" />
            </linear>
            <linear>
                <text text="挖藏宝图超时时间(s)" />
                <input id="outTime_wcbt" textSize="12" inputType="number" width="100px" singleLine="true" />
            </linear>
            <linear>
                <text text="3星帮会超时时间(s)" />
                <input id="outTime_sxbh" textSize="12" inputType="number" width="100px" singleLine="true" />
            </linear>
            <linear>
                <text text="帮会运镖超时时间(s)" />
                <input id="outTime_bhyb" textSize="12" inputType="number" width="100px" singleLine="true" />
            </linear>
            <linear>
                <text text="ORC服务器IP" />
                <input id="ip_ORC" textSize="12" width="400px" singleLine="true" />
            </linear>
            <linear>
                <checkbox id="zdzz_by" text="自动作战（备用）" />
                <checkbox id="hd_by" text="活动（备用）" />
            </linear>
            <linear>
                <text layout_gravity="center" text="悬浮窗横向(扇形)展开?" />
                <radiogroup orientation="horizontal" marginLeft="20">
                    <radio id="xfan1" text="横向" checked="true" />
                    <radio id="xfan2" text="扇形" />
                </radiogroup>
            </linear>
        </vertical>
        <vertical layout_weight="1" gravity="bottom|center">
            <text gravity="bottom|center" text="清空多触点需要切入后台重新唤醒，不必担心" />
            <linear gravity="bottom|center">
                <button
                    layout_weight="1"
                    id="qd_btn2"
                    textSize="12"
                    text="确定"
                    style="Widget.AppCompat.Button.Borderless.Colored"
                    w="auto"
                />
                <button
                    layout_weight="2"
                    id="hfmr_btn2"
                    textSize="12"
                    text="恢复默认设置"
                    style="Widget.AppCompat.Button.Borderless.Colored"
                    w="auto"
                />
                <button
                    layout_weight="1"
                    id="qx_btn2"
                    textSize="12"
                    text="取消"
                    color="red"
                    style="Widget.AppCompat.Button.Borderless.Colored"
                    w="auto"
                />
            </linear>
        </vertical>
    </vertical>
);

/**
 * 创建本地存储并初始化数据
 */
function csh() {
    variables.configure.time_reload =
        configure_page.time_reload.getText() != '' ? configure_page.time_reload.getText().toString() : 30;
    variables.configure.outTime_dmz =
        configure_page.outTime_dmz.getText() != '' ? configure_page.outTime_dmz.getText().toString() : 35;
    variables.configure.outTime_zmz =
        configure_page.outTime_zmz.getText() != '' ? configure_page.outTime_zmz.getText().toString() : 27;
    variables.configure.outTime_drfb =
        configure_page.outTime_drfb.getText() != '' ? configure_page.outTime_drfb.getText().toString() : 480;
    variables.configure.outTime_wcbt =
        configure_page.outTime_wcbt.getText() != '' ? configure_page.outTime_wcbt.getText().toString() : 35;
    variables.configure.outTime_sxbh =
        configure_page.outTime_sxbh.getText() != '' ? configure_page.outTime_sxbh.getText().toString() : 60;
    variables.configure.outTime_bhyb =
        configure_page.outTime_bhyb.getText() != '' ? configure_page.outTime_bhyb.getText().toString() : 400;
    variables.configure.ip_ORC =
        configure_page.ip_ORC.getText() != ''
            ? configure_page.ip_ORC.getText().toString().trimLeft()
            : 'http://737e8587.r24.cpolar.top';
    variables.configure.zdzz_by = configure_page.zdzz_by.isChecked() ? true : false;
    variables.configure.hd_by = configure_page.hd_by.isChecked() ? true : false;
    if (configure_page.xfan1.isChecked()) {
        variables.configure.xfan1 = true;
        variables.configure.xfan2 = false;
    } else {
        variables.configure.xfan2 = true;
        variables.configure.xfan1 = false;
    }
}
csh();
setInterval(() => {
    csh();
}, 1000);
storage.contains('configure') ? null : storage.put('configure', variables.configure);
configure_page.time_reload.setText(String(storage.get('configure').time_reload));
configure_page.outTime_dmz.setText(String(storage.get('configure').outTime_dmz));
configure_page.outTime_zmz.setText(String(storage.get('configure').outTime_zmz));
configure_page.outTime_drfb.setText(String(storage.get('configure').outTime_drfb));
configure_page.outTime_wcbt.setText(String(storage.get('configure').outTime_wcbt));
configure_page.outTime_sxbh.setText(String(storage.get('configure').outTime_sxbh));
configure_page.outTime_bhyb.setText(String(storage.get('configure').outTime_bhyb));
configure_page.ip_ORC.setText(String(storage.get('configure').ip_ORC));
Boolean(storage.get('configure').zdzz_by)
    ? configure_page.zdzz_by.setChecked(true)
    : configure_page.zdzz_by.setChecked(false);
Boolean(storage.get('configure').hd_by)
    ? configure_page.hd_by.setChecked(true)
    : configure_page.hd_by.setChecked(false);
Boolean(storage.get('configure').xfan1) ? configure_page.xfan1.setChecked(true) : configure_page.xfan2.setChecked(true);
//从本地存储获取值作为配置信息;
var t = storage.get('configure').time_reload;
var time1 = 0;
thread1 = threads.start(() => {
    //重启清多触点儿
    setInterval(() => {
        time1++;
        if (time1 % 290 == 0) {
            device.wakeUpIfNeeded(); //唤醒屏幕
        }
        if (time1 % (t * 60) == 0) {
            home();
            app.launchApp('新天龙八部');
        }
        time1 % 600 == 0 ? console.log(time1 / 60 + '/' + t) : null;
    }, 1000);
});

//确定按钮
configure_page.qd_btn2.on('click', () => {
    if (configure_page.time_reload.getText() >= 10) {
        storage.put('configure', variables.configure);
        console.log(storage.get('configure'));
        ui.setContentView(layout1);
    } else {
        toast('清空多触点儿时间清设置大于10分钟');
    }
});
configure_page.hfmr_btn2.on('click', () => {
    storage.clear();
    csh();
    toast('已恢复默认设置');
    ui.setContentView(layout1);
});
//取消按钮
configure_page.qx_btn2.on('click', () => {
    ui.setContentView(layout1);
});
exports.configure_page = configure_page;
